package com.yxr.affairs;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.umeng.analytics.MobclickAgent;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;

import com.yxr.affairs.api.model.child.Accont;
import com.yxr.affairs.base.BaseSwipeBackActivity;

import java.util.ArrayList;
import butterknife.BindView;
import jp.wasabeef.recyclerview.adapters.SlideInBottomAnimationAdapter;


/**
 * Created by chengzhuhai on 2017/12/4.
 */

public class MyAccountActivity extends BaseSwipeBackActivity {

    @BindView(R.id.trade_record_goback)
    ImageButton tradeRecordGoback;

    @BindView(R.id.rv_invest1)
    RecyclerView rvInvest1;

    @BindView(R.id.trade_record_hint)
    TextView tradeRecordHint;

    private int page = 1;
    private int epage = 20;

    private ArrayList<Accont.ListBean> mList=new ArrayList<>();
    private LinearLayoutManager mLayoutManager;

    private  AccontAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.trade_record_activity;
    }

    @Override
    protected void initViews() {

        adapter = new AccontAdapter(mContext, mList);
        SlideInBottomAnimationAdapter slideAdapter = new SlideInBottomAnimationAdapter(adapter);
        slideAdapter.setDuration(500);
        rvInvest1.setAdapter(slideAdapter);
        mLayoutManager = new LinearLayoutManager(mContext);
        rvInvest1.setLayoutManager(mLayoutManager);

        tradeRecordGoback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _exit();
            }
        });

    }


    @Override
    protected void updateViews(boolean isRefresh) {

        if (!isRefresh) {
            showLoading();
        }
      //  getMyAccountActivity();
    }




    private void loadAccont(Accont response){

        if (mList.size() > 0) {
            mList.clear();
        }

        mList.addAll(response.getList());
        adapter.notifyDataSetChanged();
        if (mList.size() > 0) {
            tradeRecordHint.setVisibility(View.GONE);
        }else{
            tradeRecordHint.setVisibility(View.VISIBLE);
        }


    }

    public  class AccontAdapter extends RecyclerView.Adapter<AccontAdapter.ViewHolder>{

        private ArrayList<Accont.ListBean>  mList;
        private Context mContext;
        public AccontAdapter( Context context, ArrayList<Accont.ListBean> list){

            mContext=context;
            mList=list;
        }
        private Context getContext() {
            return mContext;
        }

        @Override
        public AccontAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);

            View contactView = inflater.inflate(R.layout.trade_record_listview, parent, false);

            AccontAdapter.ViewHolder viewHolder = new AccontAdapter.ViewHolder(contactView);
            return viewHolder;
        }
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {


            holder.time.setText(mList.get(position).getAddtime());
            holder.remark.setText(mList.get(position).getRemark());
            holder.balance.setText(mList.get(position).getBalance());
            String balance_new = mList.get(position).getBalance_new();

            float b_new = Float.parseFloat(balance_new);
            String await_new = mList.get(position).getAwait_new();
            float a_new = Float.parseFloat(await_new);
            String tender_success_frost = mList.get(position).getFrost_new();
            if (b_new > 0) {
                holder.trade_money.setText(balance_new);
                holder.trade_record_money_layout.setVisibility(View.VISIBLE);
                holder.trade_money_dotted_line.setVisibility(View.VISIBLE);
            } else if (a_new > 0) {
                holder.trade_money.setText(await_new);
                holder.trade_record_money_layout.setVisibility(View.VISIBLE);
                holder.trade_money_dotted_line.setVisibility(View.VISIBLE);
                if (tender_success_frost.equals("tender_success_frost")) {
                    holder.trade_money_await.setText("[待收]");
                }
            } else {
                holder.trade_record_money_layout.setVisibility(View.GONE);
                holder.trade_money_dotted_line.setVisibility(View.GONE);
            }

            String expend = mList.get(position).getExpend_new();
            if (expend != null) {
                float intExpend = Float.valueOf(expend);
                if (intExpend > 0) {
                    holder.expend_new_layout.setVisibility(View.VISIBLE);
                    holder.expend_dotted_line.setVisibility(View.VISIBLE);
                    holder.expend.setText(expend);
                }else {
                    holder.expend_new_layout.setVisibility(View.GONE);
                    holder.expend_dotted_line.setVisibility(View.GONE);
                }
            }

            String frostString = mList.get(position).getFrost_new();
            if (frostString != null) {
                float intFrost = Float.valueOf(frostString);
                if (intFrost > 0) {
                    holder.frost_new_layout.setVisibility(View.VISIBLE);
                    holder.frost_dotted_line.setVisibility(View.VISIBLE);
                    holder.frost_new.setText(frostString);
                }else {
                    holder.frost_new_layout.setVisibility(View.GONE);
                    holder.frost_dotted_line.setVisibility(View.GONE);
                }
            }

        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private TextView time; //编号
            private TextView trade_money;//存入
            private TextView trade_money_await;//存入待收
            private TextView balance;//应收金额
            private TextView remark;//状态
            private TextView expend;
            private TextView frost_new;
            private LinearLayout trade_record_money_layout;
            private ImageView trade_money_dotted_line;
            private LinearLayout expend_new_layout;
            private ImageView expend_dotted_line;
            private LinearLayout frost_new_layout;
            private ImageView frost_dotted_line;

            public ViewHolder(View itemView) {
                super(itemView);
                time = (TextView) itemView.findViewById(R.id.trade_record_time);
                trade_money = (TextView)itemView.findViewById(R.id.trade_record_money);
                balance = (TextView)itemView.findViewById(R.id.trade_record_balance);
                remark = (TextView)itemView.findViewById(R.id.trade_record_remark);
                trade_money_await = (TextView)itemView.findViewById(R.id.trade_record_money_await);
                expend = (TextView)itemView.findViewById(R.id.trade_expend);
                frost_new = (TextView)itemView.findViewById(R.id.trade_frost_new);
                trade_record_money_layout= (LinearLayout) itemView.findViewById(R.id.trade_record_money_layout);
                trade_money_dotted_line= (ImageView) itemView.findViewById(R.id.trade_money_dotted_line);
                expend_new_layout= (LinearLayout) itemView.findViewById(R.id.expend_new_layout);
                expend_dotted_line= (ImageView) itemView.findViewById(R.id.expend_dotted_line);
                frost_new_layout= (LinearLayout) itemView.findViewById(R.id.frost_new_layout);
                frost_dotted_line= (ImageView) itemView.findViewById(R.id.frost_dotted_line);
            }
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            _exit();
        }
        return super.onKeyDown(keyCode, event);
    }
    private void _exit() {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.hold, R.anim.slide_right_exit);
    }
    @Override
    protected void onError_(Throwable e) {
        snackbar("网络链接失败");
    }

    @Override
    protected void onCancel_() {
        snackbar("取消");

    }
    @Override
    protected void onResume() {

        super.onResume();
        MobclickAgent.onResume(this);

    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
